﻿namespace Tszy.Unlimited.Base.Extensions;

/// <summary>
///
/// </summary>
public static class DecimalExtensions
{
    /// <summary>
    /// 保留小数（忽略尾数）。
    /// </summary>
    /// <param name="source">原数字。</param>
    /// <param name="number">保留的小数位数。</param>
    /// <returns></returns>
    public static decimal MathRound(this decimal source, int number = 0)
    {
        if (number <= 0)
            return (int)source;

        var pow = 10;

        for (var index = 0; index < number - 1; index++)
        {
            pow *= 10;
        }

        var target = ((int)(source * pow) + 0.0m) / pow;

        return target;
    }

    /// <summary>
    /// 金额类型保留2位小数（忽略尾数）。
    /// </summary>
    /// <param name="source">原数字。</param>
    /// <returns>保留 2 位小数的金额。</returns>
    public static decimal GetMoney(this decimal source)
    {
        return source.MathRound(2);
    }
}